package com.asset.dispoal.defaultCompanyMenu.service.commom;

import com.asset.dispoal.defaultCompanyMenu.domain.db.entity.GbMenuCompany;
import com.asset.dispoal.defaultCompanyMenu.domain.db.mapper.GbMenuCompanyMapper;
import com.asset.dispoal.defaultCompanyMenu.dto.GbMenuCompanyRequest;
import com.asset.dispoal.defaultCompanyMenu.service.converter.MenuCompanyConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/14$
 */
@Component
public class MenuCompanyInsertCommand implements BaseCommand<List<GbMenuCompanyRequest>, Result> {

    @Autowired
    GbMenuCompanyMapper gbMenuCompanyMapper;


    @Override
    @Transactional
    public Result execute(List<GbMenuCompanyRequest> gbMenuCompanies) {
        if (CollectionUtil.isNotEmpty(gbMenuCompanies)) {
            final List<GbMenuCompany> companyList = MenuCompanyConverter.INSTANCE.resToDo(gbMenuCompanies);
            final String companyType = companyList.get(0).getCompanyType();
            gbMenuCompanyMapper.delBatch(companyType);
            gbMenuCompanyMapper.saveBatch(companyList);
        }
        return Result.ok();
    }
}
